package org.xhy.sniffer.interceptor.enhance;


import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.This;
import org.xhy.sniffer.interceptor.loader.InterceptorInstanceLoader;


/**
 * 封装构造方法增强细节流程
 */
@Slf4j
public class ConstructorInter {
    private ConstructorInterceptor interceptor;

    /**
     *
     * @param methodsInterceptor 是ConstructorInterceptor的实现类
     * @param classLoader
     */
    public ConstructorInter(String methodsInterceptor, ClassLoader classLoader) {
        try {
            interceptor = InterceptorInstanceLoader.load(methodsInterceptor,classLoader);
        }catch (Exception e){
            log.error("无法加载拦截器:{}",methodsInterceptor);
        }
    }

    @RuntimeType
    public void intercept(@This Object obj
            ,@AllArguments Object[] allArguments) throws Throwable{
        try {
            interceptor.onConstruct(allArguments);
        }catch (Throwable t) {
            log.error("构造方法拦截器报错",t);
        }
    }
}
